package com.yinhai.message.manage.statistical.dao;

import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.core.po.MessageDetailPo;
import com.yinhai.message.manage.statistical.vo.MessageAnalysisVo;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author:TANQINGPING
 * @version:1.0 2017/4/18
 * package:com.yinhai.message.manage.statistical
 * <p>Title: MessageAnalysisDaoImpl.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 四川久远银海软件股份有限公司</p>
 */

public class MessageAnalysisDaoImpl extends HibernateDAO<MessageDetailPo> implements IMessageAnalysisDao {
    @Override
    public List<MessageAnalysisVo> getAnalysisInfo(String startDate, String endDate, String sendType) {
        StringBuffer str = new StringBuffer();
        str.append("SELECT new com.yinhai.message.manage.statistical.vo.MessageAnalysisVo(to_char(a.senddate,'yyyy-mm-dd') as type,count(a.id.mgid) as count) FROM MessageDetailPo a WHERE 1=1");
        boolean b = false;
        boolean e = false;
        boolean f = false;
        boolean g = false;
        if (!StringUtils.isEmpty(startDate)) {
            str.append(" and  a.senddate >= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            b = true;
            e = true;
        }
        if (!StringUtils.isEmpty(endDate)) {
            str.append(" and  a.senddate <= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            b = true;
            f = true;
        }
        if (!StringUtils.isEmpty(sendType)) {
            str.append(" and  a.id.sendertype = ?");
            b = true;
            g = true;
        }
        str.append(" GROUP BY to_char(a.senddate,'yyyy-mm-dd')");
        if (b) {
            return getMessageAnalysisVos(startDate, endDate, sendType, str, e, f, g);
        }
        return super.selectFildsList(str.toString());
    }

    @Override
    public List<MessageAnalysisVo> getAnalysisInfoByH(String startDate, String endDate, String sendType) {
        StringBuffer str = new StringBuffer();
        str.append("SELECT new com.yinhai.message.manage.statistical.vo.MessageAnalysisVo(to_char(a.senddate,'yyyy-mm-dd HH24') as type,count(a.id.mgid) as count) FROM MessageDetailPo a WHERE 1=1");
        boolean b = false;
        boolean e = false;
        boolean f = false;
        boolean g = false;
        if (!StringUtils.isEmpty(startDate)) {
            str.append(" and  a.senddate >= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            b = true;
            e = true;
        }
        if (!StringUtils.isEmpty(endDate)) {
            str.append(" and  a.senddate <= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
            b = true;
            f = true;
        }
        if (!StringUtils.isEmpty(sendType)) {
            str.append(" and  a.id.sendertype = ?");
            b = true;
            g = true;
        }
        str.append(" GROUP BY to_char(a.senddate,'yyyy-mm-dd HH24')");
        if (b) {
            return getMessageAnalysisVos(startDate, endDate, sendType, str, e, f, g);
        }
        return super.selectFildsList(str.toString());
    }

    @Override
    public List<MessageAnalysisVo> getTypesStatistic(String begin, String end, String sendType) {
        StringBuffer str = new StringBuffer();
        str.append("SELECT new com.yinhai.message.manage.statistical.vo.MessageAnalysisVo(b.codeDESC as type,count(a.id.mgid) as count) " +
                "FROM MessageDetailPo a,com.yinhai.modules.codetable.domain.po.AA10Po b WHERE b.id.codeType='SENDMODE' and b.id.codeValue = a.id.sendertype");
        str.append(" and a.senddate >= to_date(?,'yyyy-mm-dd HH24:MI:ss')");
        str.append(" and a.senddate <= to_date(?,'yyyy-mm-dd HH24:MI:ss')");//sendType
        if (!StringUtils.isEmpty(sendType)) {
            str.append(" and a.id.sendertype = ?");
        }
        str.append(" GROUP BY b.codeDESC");
        if (!StringUtils.isEmpty(sendType)) {
            return super.selectFildsList(str.toString(), begin, end, sendType);
        }
        return super.selectFildsList(str.toString(), begin, end);
    }

    private List<MessageAnalysisVo> getMessageAnalysisVos(String startDate, String endDate, String sendType, StringBuffer str, boolean e, boolean f, boolean g) {
        if (e && f && !g) {
            return super.selectFildsList(str.toString(), startDate, endDate);
        } else if (e && !f && g) {
            return super.selectFildsList(str.toString(), startDate, sendType);
        } else if (!e && f && g) {
            return super.selectFildsList(str.toString(), endDate, sendType);
        } else if (!e && !f && g) {
            return super.selectFildsList(str.toString(), sendType);
        } else if (!e && f && !g) {
            return super.selectFildsList(str.toString(), endDate);
        } else if (e && !f && !g) {
            return super.selectFildsList(str.toString(), startDate);
        }
        return super.selectFildsList(str.toString(), startDate, endDate, sendType);
    }
}
